package com.na.onlineservey

import com.na.onlineservey.question.CreateQuestionAction
import com.na.onlineservey.question.DeleteQuestionAction
import com.na.onlineservey.question.ListQuestionAction
import com.na.onlineservey.question.UpdateQuestionAction
import com.na.onlineservey.question.ReadQuestionAction
import com.na.onlineservey.question.SearchQuestionAction

import com.docu.common.Message
import org.springframework.beans.factory.annotation.Autowired
import grails.converters.JSON

class QuestionController {

    @Autowired
    private CreateQuestionAction createQuestionAction
    @Autowired
    private UpdateQuestionAction updateQuestionAction
    @Autowired
    private ListQuestionAction listQuestionAction
    @Autowired
    private DeleteQuestionAction deleteQuestionAction
    @Autowired
    private ReadQuestionAction readQuestionAction
    @Autowired
    private SearchQuestionAction searchQuestionAction

    static allowedMethods = [create: "POST", update: "POST", delete: "POST"]

    def list = {
        List list = listQuestionAction.execute(params, null)
        render listQuestionAction.postCondition(null, list) as JSON
    }

    def show = {
        Question questionInstance = new Question()
        render(template: "show", model: [questionInstance: questionInstance])
    }

    def create = {
        Object object = createQuestionAction.preCondition(params, null)
        Message message = null
        if (object instanceof Question) {
            message = createQuestionAction.getValidationErrorMessageForUI(object)
        } else if (object instanceof Map) {
            int rowCount = createQuestionAction.execute(null, object)
            if (rowCount>0) {
                message = createQuestionAction.getSuccessMessageForUI(new Question(), createQuestionAction.SUCCESS_SAVE)
            } else {
                message = createQuestionAction.getErrorMessageForUI(new Question(), createQuestionAction.FAIL_SAVE)
            }
        }
        else {
            message = createQuestionAction.getErrorMessageForUI(new Question(), createQuestionAction.FAIL_SAVE)
        }
        render message as JSON
    }

    def edit = {
        render readQuestionAction.execute(params, null) as JSON
    }

    def update = {
        Question questionInstance = new Question(params)
        Object object = updateQuestionAction.preCondition(params, null)
        Message message = null
        if (object == false) {
            message = updateQuestionAction.getValidationErrorMessageForUI(questionInstance)
        } else {
            int noOfRows = (int) updateQuestionAction.execute(null, object)
            if (noOfRows > 0) {
                message = updateQuestionAction.getSuccessMessageForUI(questionInstance, updateQuestionAction.SUCCESS_UPDATE)
            } else {
                message = updateQuestionAction.getErrorMessageForUI(questionInstance, updateQuestionAction.FAIL_UPDATE)
            }
        }
        render message as JSON
    }

    def delete = {
//        Question questionInstance = new Question(params)
//        questionInstance.id = Long.parseLong(params.id)
//        questionInstance.version = Long.parseLong(params.version)
        Question questionInstance = deleteQuestionAction.preCondition(params, null)
        Message message = null
        if (questionInstance) {
            int rowCount = (int) deleteQuestionAction.execute(null, questionInstance)
            if (rowCount > 0) {
                message = deleteQuestionAction.getSuccessMessageForUI(questionInstance, deleteQuestionAction.SUCCESS_DELETE)
            } else {
                message = deleteQuestionAction.getErrorMessageForUI(questionInstance, deleteQuestionAction.FAIL_DELETE)
            }
        }
        else {
            message = deleteQuestionAction.getErrorMessageForUI(questionInstance, deleteQuestionAction.ALREADY_DELETED)
        }
        render message as JSON
    }

    def search = {
        Question questionInstance = searchQuestionAction.execute(params.fieldName, params.fieldValue)
        if (questionInstance) {
            render questionInstance as JSON
        }
        else {
            render ''
        }

    }

    def genQuestionFirstStep = {
        Question questionInstance = new Question()
        render(template: "genQuestionFirstStep", model: [questionInstance: questionInstance])
    }
    def genQuestionSecondStep = {
        render(template: "genQuestionSecondStep", model: [projectID: params.project.id])
    }
}
